<?php
namespace App\Http\Controllers;

use App\Http\Logic\OrderLogic;
use App\Http\Logic\TickersLogic;
use App\Http\Requests\Order\CreateOrderRequest;
use App\Http\Requests\Order\OrderListRequest;

class OrderController extends BaseController
{
    public function getSwtBuyOrders()
    {
        $count = 5;

        $info = TickersLogic::getSwtChains();

        $lastPrice = $info['last'];

        $buy_order = [];

        $sell_orders = [];

        for($i = 0; $i < $count; $i++) {
            array_push( $buy_order, [
                "price"   => bcsub($lastPrice, '0.0000'.$i, 5),
                "size"    => rand(1, 10000),
            ] );
            array_unshift( $sell_orders, [
                "price"   => bcadd($lastPrice, '0.0000'.$i + 1, 5),
                "size"    => rand(1, 10000),
            ] );
        }

        $data = [
            "lastPrice"   => $lastPrice,
            "estimateCNY" => $lastPrice,
            "buyOrders"   => $buy_order,
            "sellOrders"  => $sell_orders
        ];

        return $this->success($data);
    }

    public function create(CreateOrderRequest $request)
    {
        $params = $request->all();

        $params['user_id'] = auth() -> user() -> id;

        $res = OrderLogic::create($params);

        if(false === $res) {
            return $this->error(OrderLogic::getErrorMessage());
        }

        return $this->success();
    }

    public function list(OrderListRequest $request)
    {
        $pageSize = $request -> input('page_size', 15);

        $pageNo = $request -> input('page_no', 1);

        $user_id = auth() -> user() -> id;

        $list = OrderLogic::list($user_id,$pageSize,$pageNo);

        if(false === $list) {
            return $this->error(OrderLogic::getErrorMessage());
        }

        return $this->success($list);
    }
}
